<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism\Router\Route {
	/**
	 * CompostiteTest
	 *
	 * Composite route test case.
	 *
 	 * @package Mechanism\Router\Route
 	 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
	 */
	class CompositeTest extends \PHPUnit_Framework_TestCase {
		/**
		 * Composite route for test
		 *
		 * @var Mechanism\Router\Route\Composite
		 */
		protected $route = null;

		/**
		 * Host route for test
		 *
		 * @var Mechanism\Router\Route\Host
		 */
		protected $hostRoute = null;

		/**
		 * Uri route for test
		 *
		 * @var Mechanism\Router\Route\Uri
		 */
		protected $uriRoute = null;

		/**
		 * Setup method
		 */
		public function setUp( ) {
			/* Routes for tests */
			$this->hostRoute = new Host(
				'[module]{s}{h:example.com}',
				array( 'module' => 'main' )
			);

			$this->uriRoute = new Uri(
				'[lang]{s}[controller]{s}[action]',
				array(
					'lang' => 'en',
					'controller' => 'index',
					'action' => 'index'
				)
			);

			/* Composite */
			$this->route = new Composite( );
		}

		/**
		 * Test for add( IRoute $route )
		 *
		 * @test
		 * @return Composite $rote
		 */
		public function add( ) {
			/* Test function and its fluent interface */
			$this->assertSame(
				$this->route,
				$this->route->add( $this->hostRoute )
			);

			$this->assertSame(
				$this->route,
				$this->route->add( $this->uriRoute )
			);

			return $this->route;
		}

		/**
		 * Test for remove( $name )
		 *
		 * @test
		 * @depends add
		 * @param Composite $rote
		 */
		public function remove( $route ) {
			/* Test function and its fluent interface */
			$this->assertSame(
				$route,
				$route->remove( 'Uri' )
			);

			try {
				/* Try to remove unexistent at now */
				$route->remove( 'Uri' );
			} catch ( Exception\Invalid $e ) {
				return;
			}
			
			$this->fail('An expected Invalid exception has not been raised.');
		}

		/**
		 * Test for set( $name, $value )
		 *
		 * @test
		 * @return Composite $route
		 */
		public function set( ) {
			$route = $this->route;

			/* Test function and its fluent interface */
			$this->assertSame(
				$route,
				$route->set( 'foo', 'bar' )
			);

			return $route;
		}

		/**
		 * Test for get( $name = null )
		 *
		 * @test
		 * @depends set
		 * @param Composite $route
		 */
		public function get( $route ) {
			/* Test function and its fluent interface */
			$this->assertEquals(
				'bar',
				$route->get( 'foo' )
			);

			/* Get all before matching */
			$this->assertEquals(
				array( 'foo' => 'bar' ),
				$route->get( )
			);				
		}

		/**
		 * Test for match( $path )
		 *
		 * @test
		 */
		public function match( ) {
			$this->route->add( $this->hostRoute )
						->add( $this->uriRoute );
			
			$testUrlA =  'http://www.example.com/ru/';
			$testUrlAResult = array(
				'module' => 'main',
				'lang' => 'ru',
				'controller' => 'index',
				'action' => 'index'
			);

			$testUrlB =  'http://music.example.com/ru/artists';
			$testUrlBResult = array(
				'module' => 'music',
				'lang' => 'ru',
				'controller' => 'artists',
				'action' => 'index'
			);

			/* Test A url */
			$this->assertTrue(
				$this->route->match( $testUrlA )
			);

			$this->assertEquals(
				$testUrlAResult,
				$this->route->get( )
			);

			/* Test B url */
			$this->assertTrue(
				$this->route->match( $testUrlB )
			);

			$this->assertEquals(
				$testUrlBResult,
				$this->route->get( )
			);
		}
	}
}























